# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=libvirt-glib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="libvirt GLib and GObject mapping library (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         #"${MINGW_PACKAGE_PREFIX}-libssp" # disabled due to gir issue
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libvirt")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!debug' 'staticlibs')
license=("LGPL 2.1")
url="https://www.gnome.org/"
#install=${_realname}-${CARCH}.install
source=(https://libvirt.org/sources/glib/${_realname}-${pkgver}.tar.xz{,.asc}
        001-disable-glib-max-version.patch
        002-disable-stack-protector-windows.patch)
sha256sums=('8423f7069daa476307321d1c11e2ecc285340cd32ca9fc05207762843edeacbd'
            'SKIP'
            'e61628e47a80adc66e215a81c8d4eeac4b556b85c3bc96553a8ab6dc49e8f33b'
            '5861775ea76ce66219d0494a726a59ecc8015e89be3f4ec4c3765fe561c76cda')
validpgpkeys=('DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF') # Daniel P. Berrange

prepare() {
  cd ${_realname}-${pkgver}

  patch -Nbp1 -i "${srcdir}/001-disable-glib-max-version.patch"
  # https://gitlab.gnome.org/GNOME/gobject-introspection/-/issues/176,
  # via https://github.com/mesonbuild/meson/issues/1910.
  # Blowing up with -fstack-protector on mingw for the same reasons
  patch -Nbp1 -i "${srcdir}/002-disable-stack-protector-windows.patch"
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=enabled \
    -Dgit_werror=disabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
